#include "misc_processing.hpp"

cv::Mat complex_abs(cv::Mat complex_mat) {
    cv::Mat ans(complex_mat.rows, complex_mat.cols, CV_64F);
    auto p = complex_mat.begin<cv::Vec2d>();
    auto q = ans.begin<double>();
    while (p != complex_mat.end<cv::Vec2d>()) {
        *q = std::sqrt(((*p)[0])*((*p)[0]) + ((*p)[1])*((*p)[1]));
        ++p;
        ++q;
    }
    return ans;
}

cv::Vec3d area_avg(cv::Mat mat, cv::Rect area) {
    int row_begin = area.y;
    int row_end = row_begin + area.height;
    int col_begin = area.x;
    int col_end = col_begin + area.width;
    cv::Vec3d ans({0.0, 0.0, 0.0});
    for (int row=row_begin; row<row_end; ++row) {
        for (int col=col_begin; col<col_end; ++col) {
            ans += mat.at<cv::Vec3b>(row, col);
        }
    }
    ans /= (row_end-row_begin) * (col_end-col_begin);
    return ans;
}